﻿using System;
using System.Data;
using System.Collections.Generic;
using Things.Common;
using Things.Model;
using Things.DALFactory;
using Things.IDAL;
using System.Net;
using System.IO;
using System.Text;
namespace Things.BLL
{
	/// <summary>
	/// GoodsOrders
	/// </summary>
	public partial class GoodsOrders
	{
		private readonly IGoodsOrders dal=DataAccess.CreateGoodsOrders();
		public GoodsOrders()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid GoodsOrdersID)
		{
			return dal.Exists(GoodsOrdersID);
		}
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string buyerAccount,Guid GoodsPriceID)
        {
            return dal.Exists(buyerAccount, GoodsPriceID);
        }

        public int GetAllSuccessfulOrder(string userAccount, Guid GoodsPriceID)
        {
            return dal.GetAllSuccessfulOrder(userAccount, GoodsPriceID);
        }
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Things.Model.GoodsOrders model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Things.Model.GoodsOrders model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid GoodsOrdersID)
		{
			
			return dal.Delete(GoodsOrdersID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string GoodsOrdersIDlist )
		{
			return dal.DeleteList(GoodsOrdersIDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Things.Model.GoodsOrders GetModel(Guid GoodsOrdersID)
		{
			
			return dal.GetModel(GoodsOrdersID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Things.Model.GoodsOrders GetModelByCache(Guid GoodsOrdersID)
		{
			
			string CacheKey = "GoodsOrdersModel-" + GoodsOrdersID;
			object objModel = Things.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(GoodsOrdersID);
					if (objModel != null)
					{
						int ModelCache = Things.Common.ConfigHelper.GetConfigInt("ModelCache");
						Things.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Things.Model.GoodsOrders)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByBuyerAccount(string buyerAccount)
        {
            return dal.GetListByBuyerAccount(buyerAccount);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetGoodsListByGoodsOrderID(Guid goodsOrderID)
        {
            return dal.GetGoodsListByGoodsOrderID(goodsOrderID);
        }
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		
        /// <summary>
        /// 获得对象列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Things.Model.GoodsOrders> GetModels(string strWhere)
        {
            return dal.GetModels(strWhere);
        }

        /// <summary>
        /// 订单查询
        /// </summary>
        /// <param name="buyerAccount"></param>
        /// <param name="filterDate"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public DataSet GetList(string buyerAccount, DateTime filterDate, string keyword)
        {
            return dal.GetList(buyerAccount,filterDate, keyword);
        }

        /// <summary>
        /// 获得商品销售记录
        /// </summary>
        /// <param name="goodsPriceID"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public DataSet GetList(Guid goodsPriceID, string status)
        {
            return dal.GetList(goodsPriceID, status);
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Things.Model.GoodsOrders> DataTableToList(DataTable dt)
		{
			List<Things.Model.GoodsOrders> modelList = new List<Things.Model.GoodsOrders>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Things.Model.GoodsOrders model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Things.Model.GoodsOrders();
					if(dt.Rows[n]["GoodsOrdersID"]!=null && dt.Rows[n]["GoodsOrdersID"].ToString()!="")
					{
						model.GoodsOrdersID=new Guid(dt.Rows[n]["GoodsOrdersID"].ToString());
					}
					if(dt.Rows[n]["BuyerAccount"]!=null && dt.Rows[n]["BuyerAccount"].ToString()!="")
					{
					model.BuyerAccount=dt.Rows[n]["BuyerAccount"].ToString();
					}
					if(dt.Rows[n]["BuyerAddress"]!=null && dt.Rows[n]["BuyerAddress"].ToString()!="")
					{
					model.BuyerAddress=dt.Rows[n]["BuyerAddress"].ToString();
					}
					if(dt.Rows[n]["TotalPrice"]!=null && dt.Rows[n]["TotalPrice"].ToString()!="")
					{
						model.TotalPrice=decimal.Parse(dt.Rows[n]["TotalPrice"].ToString());
					}
					if(dt.Rows[n]["BuyDate"]!=null && dt.Rows[n]["BuyDate"].ToString()!="")
					{
						model.BuyDate=DateTime.Parse(dt.Rows[n]["BuyDate"].ToString());
					}
					if(dt.Rows[n]["SendDate"]!=null && dt.Rows[n]["SendDate"].ToString()!="")
					{
						model.SendDate=DateTime.Parse(dt.Rows[n]["SendDate"].ToString());
					}
					if(dt.Rows[n]["CancelDate"]!=null && dt.Rows[n]["CancelDate"].ToString()!="")
					{
						model.CancelDate=DateTime.Parse(dt.Rows[n]["CancelDate"].ToString());
					}
					if(dt.Rows[n]["Status"]!=null && dt.Rows[n]["Status"].ToString()!="")
					{
					model.Status=dt.Rows[n]["Status"].ToString();
					}
					if(dt.Rows[n]["IsCompleted"]!=null && dt.Rows[n]["IsCompleted"].ToString()!="")
					{
						if((dt.Rows[n]["IsCompleted"].ToString()=="1")||(dt.Rows[n]["IsCompleted"].ToString().ToLower()=="true"))
						{
						model.IsCompleted=true;
						}
						else
						{
							model.IsCompleted=false;
						}
					}
					if(dt.Rows[n]["ModifiedDate"]!=null && dt.Rows[n]["ModifiedDate"].ToString()!="")
					{
						model.ModifiedDate=DateTime.Parse(dt.Rows[n]["ModifiedDate"].ToString());
					}
                    if (dt.Rows[n]["FaPiaoXinXi"] != null)
                    {
                        model.FaPiaoXinXi = dt.Rows[n]["FaPiaoXinXi"].ToString();
                    }
                    if (dt.Rows[n]["GoodsOrderNumber"] != null && dt.Rows[n]["GoodsOrderNumber"].ToString() != "")
                    {
                        model.GoodsOrderNumber = int.Parse(dt.Rows[n]["GoodsOrderNumber"].ToString());
                    }

					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

        /// <summary>
        /// 获得销售排行榜
        /// </summary>
        /// <param name="Top"></param>
        /// <returns></returns>
        public DataSet GetTopSaledList(int Top)
        {
            return dal.GetTopSaledList(Top);
        }

        /// <summary>
        /// 获得对象
        /// </summary>
        /// <param name="GoodsOrderNumber"></param>
        /// <returns></returns>
        public Things.Model.GoodsOrders GetModel(int GoodsOrderNumber)
        {
            return dal.GetModel(GoodsOrderNumber);
        }

        /// <summary>
        /// 获得快递信息
        /// </summary>
        /// <param name="KuaiDiGongSi"></param>
        /// <param name="KuaiDiDanHao"></param>
        /// <param name="show"> 返回类型： 0：返回json字符串， 1：返回xml对象， 2：返回html对象， 3：返回text文本。默认值: 2</param>
        /// <param name="muti">返回信息数量： 1:返回多行完整的信息， 0:只返回一行信息。默认值: 1</param>
        /// <returns></returns>
        public string GetKuaiDiXinXi(string KuaiDiGongSi, string KuaiDiDanHao, string show="2", string muti="1" )
        {
            string detail = string.Empty;

            string ApiKey = Things.Common.ConfigureValue.KuaiDiKey;//请把XXXXXX修改成您在快递100网站申请的APIKey
            string typeCom = KuaiDiGongSi; //Request["com"];
            string nu = KuaiDiDanHao; // Request["nu"];

            if (typeCom == "AAE全球专递")
            {
                typeCom = "aae";
            }
            if (typeCom == "安捷快递")
            {
                typeCom = "anjiekuaidi";
            }
            if (typeCom == "安信达快递")
            {
                typeCom = "anxindakuaixi";
            }
            if (typeCom == "百福东方")
            {
                typeCom = "baifudongfang";
            }
            if (typeCom == "彪记快递")
            {
                typeCom = "biaojikuaidi";
            }
            if (typeCom == "BHT")
            {
                typeCom = "bht";
            }
            if (typeCom == "BHT")
            {
                typeCom = "bht";
            }
            if (typeCom == "希伊艾斯快递")
            {
                typeCom = "cces";
            }
            if (typeCom == "中国东方")
            {
                typeCom = "Coe";
            }
            if (typeCom == "长宇物流")
            {
                typeCom = "changyuwuliu";
            }
            if (typeCom == "大田物流")
            {
                typeCom = "datianwuliu";
            }
            if (typeCom == "德邦物流")
            {
                typeCom = "debangwuliu";
            }
            if (typeCom == "DPEX")
            {
                typeCom = "dpex";
            }
            if (typeCom == "DHL")
            {
                typeCom = "dhl";
            }
            if (typeCom == "D速快递")
            {
                typeCom = "dsukuaidi";
            }
            if (typeCom == "fedex")
            {
                typeCom = "fedex";
            }
            if (typeCom == "飞康达物流")
            {
                typeCom = "feikangda";
            }
            if (typeCom == "凤凰快递")
            {
                typeCom = "fenghuangkuaidi";
            }
            if (typeCom == "港中能达物流")
            {
                typeCom = "ganzhongnengda";
            }
            if (typeCom == "广东邮政物流")
            {
                typeCom = "guangdongyouzhengwuliu";
            }
            if (typeCom == "汇通快运")
            {
                typeCom = "huitongkuaidi";
            }
            if (typeCom == "恒路物流")
            {
                typeCom = "hengluwuliu";
            }
            if (typeCom == "华夏龙物流")
            {
                typeCom = "huaxialongwuliu";
            }
            if (typeCom == "佳怡物流")
            {
                typeCom = "jiayiwuliu";
            }
            if (typeCom == "京广速递")
            {
                typeCom = "jinguangsudikuaijian";
            }
            if (typeCom == "急先达")
            {
                typeCom = "jixianda";
            }
            if (typeCom == "佳吉物流")
            {
                typeCom = "jiajiwuliu";
            }
            if (typeCom == "加运美")
            {
                typeCom = "jiayunmeiwuliu";
            }
            if (typeCom == "快捷速递")
            {
                typeCom = "kuaijiesudi";
            }
            if (typeCom == "联昊通物流")
            {
                typeCom = "lianhaowuliu";
            }
            if (typeCom == "龙邦物流")
            {
                typeCom = "longbanwuliu";
            }
            if (typeCom == "民航快递")
            {
                typeCom = "minghangkuaidi";
            }
            if (typeCom == "配思货运")
            {
                typeCom = "peisihuoyunkuaidi";
            }
            if (typeCom == "全晨快递")
            {
                typeCom = "quanchenkuaidi";
            }
            if (typeCom == "全际通物流")
            {
                typeCom = "quanjitong";
            }
            if (typeCom == "全日通快递")
            {
                typeCom = "quanritongkuaidi";
            }
            if (typeCom == "全一快递")
            {
                typeCom = "quanyikuaidi";
            }
            if (typeCom == "全峰快递")
            {
                typeCom = "quanfengkuaidi";
            }
            if (typeCom == "盛辉物流")
            {
                typeCom = "shenghuiwuliu";
            }
            if (typeCom == "速尔物流")
            {
                typeCom = "suer";
            }
            if (typeCom == "盛丰物流")
            {
                typeCom = "shengfengwuliu";
            }
            if (typeCom == "天地华宇")
            {
                typeCom = "tiandihuayu";
            }
            if (typeCom == "天天快递")
            {
                typeCom = "tiantian";
            }
            if (typeCom == "TNT")
            {
                typeCom = "tnt";
            }
            if (typeCom == "UPS")
            {
                typeCom = "ups";
            }
            if (typeCom == "万家物流")
            {
                typeCom = "wanjiawuliu";
            }
            if (typeCom == "文捷航空速递")
            {
                typeCom = "wenjiesudi";
            }
            if (typeCom == "伍圆速递")
            {
                typeCom = "wuyuansudi";
            }
            if (typeCom == "万象物流")
            {
                typeCom = "wanxiangwuliu";
            }
            if (typeCom == "新邦物流")
            {
                typeCom = "xinbangwuliu";
            }
            if (typeCom == "信丰物流")
            {
                typeCom = "xinfengwuliu";
            }
            if (typeCom == "星晨急便")
            {
                typeCom = "xingchengjibian";
            }
            if (typeCom == "鑫飞鸿物流")
            {
                typeCom = "xinhongyukuaidi";
            }
            if (typeCom == "亚风速递")
            {
                typeCom = "yafengsudi";
            }
            if (typeCom == "一邦速递")
            {
                typeCom = "yibangwuliu";
            }
            if (typeCom == "优速物流")
            {
                typeCom = "youshuwuliu";
            }
            if (typeCom == "远成物流")
            {
                typeCom = "yuanchengwuliu";
            }
            if (typeCom == "圆通速递")
            {
                typeCom = "yuantong";
            }
            if (typeCom == "源伟丰快递")
            {
                typeCom = "yuanweifeng";
            }
            if (typeCom == "元智捷诚快递")
            {
                typeCom = "yuanzhijiecheng";
            }
            if (typeCom == "越丰物流")
            {
                typeCom = "yuefengwuliu";
            }
            if (typeCom == "韵达快递")
            {
                typeCom = "yunda";
            }
            if (typeCom == "源安达")
            {
                typeCom = "yuananda";
            }
            if (typeCom == "运通快递")
            {
                typeCom = "yuntongkuaidi";
            }
            if (typeCom == "宅急送")
            {
                typeCom = "zhaijisong";
            }
            if (typeCom == "中铁快运")
            {
                typeCom = "zhongtiewuliu";
            }
            if (typeCom == "中通速递")
            {
                typeCom = "zhongtong";
            }
            if (typeCom == "中邮物流")
            {
                typeCom = "zhongyouwuliu";
            }

            if (string.IsNullOrEmpty(typeCom) == false && string.IsNullOrEmpty(nu) == false)
            {
                string apiurl = "http://api.kuaidi100.com/api?id=" + ApiKey + "&com=" + typeCom + "&nu=" + nu + "&show=" + show + "&muti=" + muti + "&order=asc";
                //Response.Write (apiurl);
                WebRequest request = WebRequest.Create(@apiurl);
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                Encoding encode = Encoding.UTF8;
                StreamReader reader = new StreamReader(stream, encode);
                detail = reader.ReadToEnd();
            }
            else
            {
                detail = "请核对物流公司信息和快递单号。";
            }

            return detail;
        }


		#endregion  Method
	}
}

